import java.util.InputMismatchException;
import java.util.Scanner;

public class GuessNumber {
    public static void main(String[] args) {
        //1.打印提示信息
        System.out.println("欢迎来到猜数字");
        while (true) {
            //2.生成一个1-100之间的答案数字
            int answer = (int) (Math.random() * 100 + 1);
            // System.out.println(answer);
            //3.接收玩家输入的猜测数字
            Scanner scan = new Scanner(System.in);
            int chances = 7;
//5.实现在玩家测错情况下可以重复不停猜测直至猜对的功能
            int guess;
            while (chances > 0) {
                System.out.println("请输入一个1-100之间的整数,含和100,你还有" + chances + "机会：");

                try {
                    guess = scan.nextInt();
                } catch (InputMismatchException e) {
                    System.out.println("请输入数字！");
                    scan.next();
                    continue;
                }
                if (guess == -1) {
                    break;
                }//4.实现比较答案数字与用户猜测数字关系后打印提示信息的功能
                else if (guess > answer) {
                    System.out.println("大了");

                } else if (guess < answer) {
                    System.out.println("小了");
                } else {
                    System.out.println("猜对了");
                    break;
                }
                //实现一轮游戏结束后玩家可以通过选择决定再玩一局还是结束游戏的功能
                chances=chances-1;
            }
            System.out.println("再玩一次吗(y/n)");
            String again =scan.next();
            if (!again.equalsIgnoreCase("y")){
                break;
            }
        }
        }
    }
